<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Exceptions\Admin\PermissionDeniedException;
use Gate;
use App\Models\GoodsStockHistory;

class GoodsStockHistoryController extends Controller
{

    public function goods_stock_history_gate()
    {
        if (!Gate::allows('stock','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->goods_stock_history_gate();

        $search = array('goods_id'=>'', 'stock_status'=>0);
        $stockStatusArr = array('0'=>'请选择操作状态', '1'=>'入库', '2'=>'出库', '3'=>'其他操作');
        $operTypeArr = array('-1'=>'请选择操作', '0' => '单个操作', '1'=>'后台批量操作');      //操作类型，1: 后台批量操作; 2:单个操作;

        $query = GoodsStockHistory::orderBy('add_time', 'desc');

        if (!empty($request->get('goods_id'))){
            $query = $query->where('goods_id', $request->get('goods_id'));
            $search['goods_id'] = $request->goods_id;
        }//end if()

        if (!empty($request->get('stock_status'))){
            $query = $query->where('stock_status', $request->get('stock_status'));
            $search['stock_status'] = $request->stock_status;
        }//end if()

        if (!empty($request->get('oper_type')) || $request->get('oper_type')>-1){
            $query = $query->where('oper_type', $request->get('oper_type'));
            $search['oper_type'] = $request->oper_type;
        }//end if()

        $goodsStockHistory = $query->paginate(10);

        return view('admin.goods.history', compact(['goodsStockHistory', 'search', 'stockStatusArr', 'operTypeArr']));

    }

    /**
     * Show the form for creating a new resource.
     * 添加库存操作记录
     *
     * @return \Illuminate\Http\Response
     */
    public function create($goods_sn, $user_id, $describe, $operation, $oper_type)
    {
        $this->goods_stock_history_gate();

        GoodsStockHistory::insert(['goods_sn' => $goods_sn, 'user_id' => $user_id, 'describe' => $describe, 'stock_status' => $operation, 'oper_type' => $oper_type, 'add_time' => time()]);

        return true;
    }//end func

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
